package com.fram.controller.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseController;
import com.fram.entity.IntegralRule;
import com.fram.entity.IntegralSignIn;
import com.fram.service.api.v1.IntegralService;

@RestController
@RequestMapping("/api/{version}/integral/signin")
public class IntegralController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(IntegralController.class);
	@Autowired
	private IntegralService integralService;

	/** 签到 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	private ResponseMsg add(@RequestBody IntegralSignIn record) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			record.setUserId(getApiUserId());
			msg.setData(integralService.add(record));
		} catch (ProException e) {
			LOG.error("-签到获取积分->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-签到获取积分-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 日历 签到标记 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/calendar", method = RequestMethod.GET)
	private ResponseMsg listdata(String date) {
		ResponseMsg msg = new ResponseMsg();
		try {
			msg.setData(integralService.listDate(getApiUserId(), date));
		} catch (ProException e) {
			LOG.error("-记录->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-记录-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	/**积分规则 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/list/integral",method = RequestMethod.POST)
	private ResponseMsg listIntegral(@RequestBody IntegralRule request) {
		ResponseMsg msg= new ResponseMsg();
		try {
			msg.setData(integralService.listIntegral(request.getRuleKey()));
		} catch (ProException e) {
			LOG.error("-规则->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-规则-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
}
